require.config({
	baseUrl: "/app",
});
require(["jquery", "ControllerBase"], function($, ControllerBase) {
    $(function() {
		var page, testObject, targetDiv, ctrlName, gProps;
		page = $('body');
		targetDiv = "testTarget";
		ctrlName = "TestControl";		
		///////////////////////////////////////////////////////
		// Utility functions.
		///////////////////////////////////////////////////////
		function begin() {

			page.append('<div id="' + targetDiv + '"></div>');
		}
		
		function before() {
			gProps = null;
			testObject = new ControllerBase(ctrlName, targetDiv, gProps); 

		}
		
		function after() { 
			page.append('&nbsp;<br/>'); 
			$('#' + targetDiv).html('')
			testObject = null; 
		}
		
		function end() {
			page.remove('#' + targetDiv);
		}

		function sleep(milliseconds) {
		  var start = new Date().getTime();
		  for (var i = 0; i < 1e7; i++) {
			if ((new Date().getTime() - start) > milliseconds){
			  break;
			}
		  }
		}

		// test function.
		function testHandler() { 
			$('#' + targetDiv).html('hello'); 
		};


		//////////////////////////////////////////////////////
        // Tests start here.
		//////////////////////////////////////////////////////

		page.append("<h3>Testing ControllerBase class</h3> <br/>");
		
		begin();
		
		// Test contructor.
		page.append("Testing Constructor, should return [Object] typeof === ControllerBase...");
		before();
		if (!(testObject instanceof ControllerBase)) {
			page.append('<b>Failed to create!</b>');
		}
		else if (testObject instanceof ControllerBase) {
			page.append('<i>Passed!</i>');			
		}
		after();
				

		page.append('Test "id" property. Should equal "' + ctrlName + '"... ');
		before();
		if (testObject.id !== ctrlName) {
			page.append('<b>Failed! Name returned "' + testObject.id + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();		
		

		
		page.append('Test "class" property. Should equal "testClass"... ');
		gProps = {'class': 'testClass'};
		before();
		if (testObject.class !== 'testClass') {
			page.append('<b>Failed! "class" returned "' + testObject.class + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		
		page.append('Test "css" property, full object return. Should equal "#fff"... ');
		gProps = {'css': {'color': '#fff', 'font-size': '12px'}};
		before();
		if (testObject.css()['color'] !== '#fff') {
			page.append('<b>Failed! "class" returned "' + testObject.css + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		
		page.append('Test "css" property, getting a specified value. Should equal "#fff"... ');
		gProps = {'css': {'color': '#fff', 'font-size': '12px'}};
		before();
		if (testObject.css('color') !== '#fff') {
			page.append('<b>Failed! "class" returned "' + testObject.css('color') + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();		
		
		page.append('Test "css" property, setting pre-initialized value. Should equal "#fff" before and "#000" after... ');
		gProps = {'css': {'color': '#fff', 'font-size': '12px'}};
		before();
		testObject.css('color', '#000'); 
		if (testObject.css('color') !== '#000') {
			page.append('<b>Failed! "class" returned "' + testObject.css('color') + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		
		page.append('Test "css" property, setting new value. Should equal "undefined" before and "#000" after... ');
		gProps = {'css': {'color': '#fff', 'font-size': '12px'}};
		before();
		testObject.css('background-color', '#000'); 
		if (testObject.css('background-color') !== '#000') {
			page.append('<b>Failed! "class" returned "' + testObject.css('background-color') + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();				 	
				
		page.append('Test "css" property, specific-value clear. "color" Should equal "#fff", before and "undefined", after... ');
		gProps = {'css': {'color': '#fff', 'font-size': '12px'}};
		before();
		testObject.css('color', ''); 
		if (typeof testObject.css('color') !== 'undefined') {
			page.append('<b>Failed! "class" returned "' + typeof testObject.css('color') + '"</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		

		page.append('Test _eventHandlers visibility. Should be invisible to outside callers, Like brother Jed... ');
		before();
		if (typeof testObject._eventHandlers !== 'undefined') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();

		page.append('Test headless init(). Should not cause an abort... ');
		gProps = {'headless': true};
		before();
		if (!testObject.init()) {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		
		page.append('Test headed init(). Should write "&lt;div id="TestControl"&gt;&lt;b&gt;Test View&lt;/b&gt;&lt;/div&gt;" to the html property of the output div.');
		before();
		testObject.init();
		// For test purposes, do a little delay.
		sleep(1000);		
		if ($('#' + targetDiv).html() !== '<div id="TestControl"><b>Test View</b></div>') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();		
		 
		page.append('Test render(). Should write "ControllerBase.render() called" to the output div... ');
		before();
		testObject.render()
		if ($('#' + targetDiv).html() !== 'ControllerBase.render() called') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();
		 
		page.append('Test hide(). Should hide the loaded HTML view, in the output div... ');
		before();
		testObject.init();		
		testObject.hide();
		if ($('#' + ctrlName).css('display') !== 'none') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');			
		}
		after();

		page.append('Test show(). Should hide the loaded HTML view, in the output div... ');
		before();
		testObject.init();
		testObject.hide();
		testObject.show();
		if ($('#' + ctrlName).css('display') !== 'block') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');
		}
		after(); 		

		page.append('Test toString(). Should return "ControllerBase"... ');
		before();
		testObject.init();
		if (testObject.toString() !== 'ControllerBase') {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');
		}
		after(); 		

		page.append('Test bind() with a valid event. If properly bound, should return "true" when first bound and "false" when attempting to bind same funtion.... ');
		before();
		testObject.init();
		if (!testObject.bind('loaded', testHandler)) {
			page.append('<b>Failed!</b>');
			
		}
		else if (testObject.bind('loaded', testHandler)) {
			page.append('<b>Failed!</b>');
			
		}
		else {
			page.append('<i>Passed!</i>');
			
		}
		after(); 	

		page.append('Test bind() with an invalid event. If properly bound, should return "false" when attempting to bind same funtion.... ');
		before();
		testObject.init();
		if (testObject.bind('disallowed_func', testHandler)) {
			page.append('<b>Failed!</b>');
		}
		else {
			page.append('<i>Passed!</i>');
		}
		after(); 	

		page.append('Test fire() with a valid event. If properly bound, should write "hello" to centent div.');
		before();
		testObject.init();
		if (testObject.bind('loaded', testHandler)) {
			testObject.fire('loaded', {name: 'hello'});
			if ($('#' + targetDiv).html() !== 'hello') {
				page.append('<b>Failed!</b>');
			}
			else {
				page.append('<i>Passed!</i>');
			}
		}
		after(); 	

		page.append('Test fire() with an invalid event. If properly detected, should NOT write "hello" to centent div.');
		before();
		testObject.init();
		if (testObject.bind('loaded', testHandler)) {
			testObject.fire('disallowed_func', {name: 'hello'});
			if ($('#' + targetDiv).html() === 'hello') {
				page.append('<b>Failed!</b>');
			}
			else {
				page.append('<i>Passed!</i>');
			}
		}
		after(); 	
		
		end();
		
    });

	
});









